package com.yongninger.activity.customer;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.yongninger.activity.R;
import com.yongninger.dao.CustomerInfoDao;
import com.yongninger.dao.ShipmentsDao;
import com.yongninger.vo.ClerkInfo;
import com.yongninger.vo.CustomerInfo;

public class AddActivity extends Activity {
	private CustomerInfoDao customerInfoDao;
	private ShipmentsDao shipmentsDao;
	private String[] arrClerk;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.customer_add);
		customerInfoDao = new CustomerInfoDao(getApplicationContext());
		/**
		 * 选择业务员
		 */
		shipmentsDao = new ShipmentsDao(getApplicationContext());
		List<ClerkInfo> listClerk = shipmentsDao.queryClerkInfoVo();
		arrClerk = new String[listClerk.size()];
		for (int i = 0; i < listClerk.size(); i++) {
			arrClerk[i] = listClerk.get(i).getCname();
		}
		Spinner spinner = (Spinner) findViewById(R.id.ccclerk);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, arrClerk);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
		spinner.setVisibility(View.VISIBLE);
		Button addBtn = (Button) this.findViewById(R.id.add);
		OnClickListener onClickListener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// 保存数据到数据库
				if (save()) {
					Toast.makeText(getApplicationContext(), "保存成功", 2).show();
					// 跳转到查询页面
					Intent intent = new Intent(AddActivity.this,
							ListActivity.class);
					startActivity(intent);
				} else {
					Toast.makeText(getApplicationContext(), "保存失败", 2).show();
				}
			}
		};
		addBtn.setOnClickListener(onClickListener);
	}

	private boolean save() {
		EditText cccname = (EditText) findViewById(R.id.cccname);
		EditText cccode = (EditText) findViewById(R.id.cccode);
		EditText cccontact = (EditText) findViewById(R.id.cccontact);
		EditText cccaddress = (EditText) findViewById(R.id.cccaddress);
		EditText cccperson = (EditText) findViewById(R.id.cccperson);
		Spinner ccclerkSpinner = (Spinner) findViewById(R.id.ccclerk);
		CustomerInfo customerInfo = new CustomerInfo();
		customerInfo.setCname(cccname.getText().toString());
		customerInfo.setCcode(cccode.getText().toString());
		customerInfo.setCaddress(cccaddress.getText().toString());
		customerInfo.setCclerk(ccclerkSpinner.getSelectedItem().toString());
		customerInfo.setCcontact(cccontact.getText().toString());
		customerInfo.setCperson(cccperson.getText().toString());
		return customerInfoDao.insertCustomerVo(customerInfo);
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			Toast.makeText(getApplicationContext(), "请先保存内容",
					Toast.LENGTH_SHORT).show();
			return false;
		}else if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() >= 0){
			finish();
			// 跳转到查询页面
			Intent intent = new Intent(AddActivity.this,
					ListActivity.class);
			startActivity(intent);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
